﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WinnerCMS.BLL;
using WinnerCMS.Model;
using WinnerCMS.Common;
using WinnerCMS.Model.Attribute;

#endregion

namespace WinnerCMS.WebUI.Admin.ControlPanel
{
    [Access("264005")]
    public partial class DefendPROC : WebFactory.PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.lblPath.Text = "控制面板 →  <a href=\"DefendPROC.aspx\">防垃圾策略</a>";

                BindList(1);

                LoadConfig();
            }
        }

        private void LoadConfig()
        {
            var config = CMSConfig.Instance();
            this.cbkCheckPost.SelectedIndex = (config.DefendPROC.CheckPost ? 0 : 1);
            this.cbkCheckCN.SelectedIndex = (config.DefendPROC.CheckCN ? 0 : 1);
            this.NtxtLinkNum.Text = config.DefendPROC.LinkNum.ToString();
            this.NtxtPostSpeed.Text = config.DefendPROC.PostSpeed.ToString();
        }


        private void BindList(int PageNo)
        {
            this.GV_List.EmptyDataTemplate = GetTemplate(this);

            var pp = new PagingParam();
            pp.PageNo = PageNo;
            pp.PageSize = this.PageNavigation1.PageSize;

            this.GV_List.DataSource = new WordsManage().BindList(1, pp);
            this.GV_List.DataBind();
            this.PageNavigation1.RecordCount = pp.RecordCount;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                var config = CMSConfig.Instance();
                config.DefendPROC.CheckPost = (this.cbkCheckPost.SelectedIndex == 0 ? true : false);
                config.DefendPROC.CheckCN = (this.cbkCheckCN.SelectedIndex == 0 ? true : false);

                config.DefendPROC.LinkNum = int.Parse(this.NtxtLinkNum.Text);
                config.DefendPROC.PostSpeed = int.Parse(this.NtxtPostSpeed.Text);
                config.Save(config);

                MsgBox.Message("保存成功！", this.UpdatePanel1);
            }
            catch (Exception ex)
            {
                MsgBox.Message("保存错误！" + ex.Message.Replace(@"\", @"\\"), this.UpdatePanel1);
            }
        }


        protected void btnAddBL_Click(object sender, EventArgs e)
        {
            Add(0, this.txtFind.Text.Trim());
        }

        #region 添加或修改

        private void Add(int ID, string Find)
        {
            var model = new Words();
            model.Id = ID;
            model.Find = Find;
            model.Replacement = "";
            model.Ban = 1;
            model.AdminName = UserManage.Current.UserName;


            string strErr = null;

            new WordsManage().Add(model, ref strErr);

            if (string.IsNullOrEmpty(strErr) == false)
            {
                MsgBox.Message(strErr, this.UpdatePanel1);
            }
            else
            {
                BindList(1);
            }
        }

        #endregion

        protected void GV_List_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var id = (int) this.GV_List.DataKeys[e.RowIndex].Value;
            var Find = ((TextBox) this.GV_List.Rows[e.RowIndex].Cells[0].Controls[0]).Text.Trim();

            Add(id, Find);

            this.GV_List.EditIndex = -1;
            BindList(1);
        }

        protected void GV_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var ID = this.GV_List.DataKeys[e.RowIndex].Value.ToString();

            new WordsManage().Delete(ID, this.UpdatePanel1);


            MsgBox.Message("删除成功!", this.UpdatePanel1);

            BindList(1);
        }

        protected void GV_List_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.GV_List.EditIndex = -1;
            BindList(1);
        }


        protected void GV_List_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.GV_List.EditIndex = e.NewEditIndex;
            BindList(this.PageNavigation1.PageNo);
        }


        protected void PageNavigation1_PageChanging(object src, WebControls.PageChangingEventArgs e)
        {
            BindList(e.NewPageIndex);
        }
    }
}